package com.tusuo.stds.ui.school;

import android.content.Context;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.AreaList;
import com.tusuo.stds.rest.SchoolList;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.util.IntentUtils;
import com.tusuo.stds.util.Utils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SchoolPresenter.class)
class SchoolPresenter extends MvpBasePresenter<SchoolView> {

    @Inject
    ApiHelper mApiHelper;

    private Context mContext;
    private String mErrMsgServer;

    private Subscriber<SchoolList> mGetSchoolListSubscriber = null;
    private Subscriber<ApiResult> mSetSchoolListSubscriber = null;

    private UserInfo mUserInfo;
    private int mAreaId;

    SchoolPresenter(Context context, UserInfo userInfo, int areaId) {
        mContext = context;
        mErrMsgServer = mContext.getResources().getString(R.string.err_msg_server);
        mUserInfo = userInfo;
        mAreaId = areaId;

        Injector.initSchoolPresenterComponent().inject(this);
    }

    @Override
    public void attachView(SchoolView view) {
        super.attachView(view);
//        loadData(mAreaId);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mGetSchoolListSubscriber != null) {
            mGetSchoolListSubscriber.unsubscribe();
            mGetSchoolListSubscriber = null;
        }
        if (mSetSchoolListSubscriber != null) {
            mSetSchoolListSubscriber.unsubscribe();
            mSetSchoolListSubscriber = null;
        }
        super.detachView(retainInstance);
    }

    void loadData(int areaId) {
        mGetSchoolListSubscriber = new GetSchoolListSubscriber();
        mApiHelper.getSchool(mGetSchoolListSubscriber, areaId);
    }

    void setSchool(SchoolList.School school) {
        mSetSchoolListSubscriber = new SetSchoolListSubscriber();
        mApiHelper.setSchool(mSetSchoolListSubscriber, school.Id, school.Name);
    }

    private class GetSchoolListSubscriber extends Subscriber<SchoolList> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final SchoolList list) {
            if (list.checkState(mContext) && getView() != null) {
                if (list.Total < 1 || list.Data.size() < 1) {
                    Timber.d("获取学校数据失败!");
                } else {
                    getView().setListViewAdapter(new SchoolListAdapter(mContext, list.Data, mUserInfo));
                }
            }
        }
    }

    private class SetSchoolListSubscriber extends Subscriber<ApiResult> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final ApiResult result) {
            if (result.checkState(mContext)) {
                IntentUtils.toAccountActivity(mContext);
//                IntentUtils.toCurrentMainActivity(mContext);
            }
        }
    }
}
